package com.example.demo.controller;

import com.example.demo.model.User;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-01-11
 * Time: 21:37
 */
@RestController
@RequestMapping("/web")
public class WebController1 {
    @GetMapping("/hi1")
    public Object sayHi1() {
        return "HI,SpringMVC";
    }

    @PostMapping("/hi2")
    public Object sayHi2() {
        return "HI,SpringMVC";
    }

    @RequestMapping("/testdemo1")
    public Object testDemo1(HttpServletRequest req, HttpServletResponse resp) {
        Integer res = Integer.valueOf(req.getParameter("name"));
        return res;
    }

    //SpringMVC获取单个参数->直接写要获取的参数即可,命名要与前段传递的参数名一致
    //拿到对应参数后,就会对当前变量赋值
    @RequestMapping("/testdemo2")
    public Object testDemo2(String name,int password){
        return "姓名 : " + name + " 密码为 : " + password;
    }

    /**
     * 如果参数过多,不好维护,后端这边就可以将其封装成一个普通的对象来接收前端的参数
     * 但是封装的对象中的属性,要与前端传来的参数一一对应餐可以接收到
     */
    @RequestMapping("/testdemo3")
    public Object testDemo3(User user) {
        return user.toString();
    }

    /**

    /**
     * 获取ajax请求中的参数
     * 前端利用JSON格式的参数,后端返回给前端JSON对象
     */
    @RequestMapping("/getajax")
    public HashMap<String, Object> getAjax(User user){
        HashMap<String,Object> res = new HashMap<>();
        res.put("name",user.getName());
        res.put("password",user.getPassword());
        return res;
    }

    /**
     * 获取用户传递的json对象
     */
    @RequestMapping("/getajax1")
    public HashMap<String, Object> getAjax1(@RequestBody User user){
        HashMap<String,Object> res = new HashMap<>();
        res.put("name",user.getName());
        res.put("password",user.getPassword());
        return res;
    }

    /**
     * 用户上传文件,后端接收文件进行接收并保存
     */

    @RequestMapping("/getfile")
    public Object getfile(@RequestPart("myfile")MultipartFile myfile) throws IOException {
        //将用户的文件保存起来->保存到本地e盘
        myfile.transferTo(new File("E:\\demoxxx.png"));
        return "上传成功";
    }

    @RequestMapping("/getcookie")
    public void getCookie(HttpServletRequest req,HttpServletResponse resp) {
        Cookie[] cookies = req.getCookies();
    }

    @RequestMapping("/getcookie1")
    public Object getCookie1(@CookieValue("cookie1") String cookie1) {
        return "cookie值为 : " + cookie1;
    }


    /**
     * Servlet的方式设置session
     */
    @RequestMapping("/setSession")
    public Object setSession(HttpServletRequest request) {
        /**
         * 没有session就创建一个
         */
        HttpSession session = request.getSession(true);
        if(session!=null) {
            session.setAttribute("username","sfx");
        }
        return "ok";
    }

    /**
     * Servlet获取session
     * @param request
     * @return
     */
    @RequestMapping("/getSession")
    public Object getSession(HttpServletRequest request) {
        /**
         * 没有不创建
         */
        HttpSession session = request.getSession(false);
        String username = "无";
        if(session!=null&&session.getAttribute("username")!=null) {
            username = (String) session.getAttribute("username");
        }
        return "username" + username;
    }

    /**
     * 获取session
     */
    @RequestMapping("/getSession1")
    public Object getSession1(@SessionAttribute(value = "username",required = false)
                                          String username) {
        return "username" + username;
    }

    @RequestMapping("/getHeader2")
    public Object getHeader2(@RequestHeader("User-Agent") String userAgent) {
        return "User-Agent : " + userAgent;
    }

    /**
     * 对前端传来的魔法值 进行重命名, 接收前端传的的us,然后进行重命名为user
     * @param user
     * @return
     */
    @RequestMapping("/getUser")
    public Object getUser(@RequestParam("us") String user) {
        return "user :"  + user;
    }

}
